﻿using AdverseEventReportSystem.BLL.ServiceReference1;
using AdverseEventReportSystem.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdverseEventReportSystem.BLL
{
    public class NurseLevelManager : BaseManager
    {
        /// <summary>
        /// 添加/编辑能级申请
        /// </summary>
        /// <param name="nurseLevel"></param>
        /// <returns></returns>
        public static OperateResult Save(NurseLevel nurseLevel)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                SysLoginUser loginUser = LoginManager.GetLoginUser();

                if (nurseLevel.Id == 0)
                {
                    nurseLevel.CreateTime = DateTime.Now;
                    nurseLevel.CreateUserId = loginUser.Personnel_id;
                }
                else
                {
                    nurseLevel.UpdateTime = DateTime.Now;
                    nurseLevel.UpdateUserId = loginUser.Personnel_id;
                }
                if (nurseLevel.Status == 1 || nurseLevel.Status == 2)
                {
                    nurseLevel.CommitTime = DateTime.Now;
                }
                nurseLevel.UserId = loginUser.Personnel_id;
                nurseLevel.DepartmentCode = loginUser.CurrDepCode;

                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfP_NurseLevel result = service.setP_NurseLevel(nurseLevel);
                if (result.S)
                {
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 获取能级晋升申请详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static OperateResult GetNurseLevelInfo(string id)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfNurseLevel result = service.getP_NurseLevelID(id);
                if (result.S)
                {
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        /// <param name="reviewReason"></param>
        /// <returns></returns>
        public static OperateResult Review(int id, int status, string reviewReason)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                Loginuser loginUser = LoginManager.GetLoginUser();
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfNurseLevel result = service.getP_NurseLevelID(id.ToString());

                if (result.S)
                {
                    NurseLevel nurseLevel = result.D;
                    if (nurseLevel.Status != 1 && nurseLevel.Status != 2)
                    {
                        resultInfo.Result = 0;
                    }

                    if (resultInfo.Result == 1)
                    {
                        nurseLevel.Status = status;
                        if (status == 2 || status == 3)
                        {
                            nurseLevel.ReviewReason = reviewReason;
                            nurseLevel.ReviewTime = DateTime.Now;
                            nurseLevel.ReviewUserId = loginUser.Personnel_id;
                        }
                        else
                        {
                            nurseLevel.ReviewReason1 = reviewReason;
                            nurseLevel.ReviewTime1 = DateTime.Now;
                            nurseLevel.ReviewUserId1 = loginUser.Personnel_id;
                        }
                        ResultHelperOfBoolean result1 = service.auditP_NurseLevel(nurseLevel);
                        if (!result1.S)
                        {
                            resultInfo.Result = 0;
                            resultInfo.Body = result1.M;
                        }
                    }
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }
    }
}
